package com.hliu.shiro.dao.impl;

import com.hliu.core.dao.hibernate4.BaseHibernateDao;
import com.hliu.shiro.dao.IUserDao;
import com.hliu.shiro.model.User;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

/**
 * author: 6
 * date: 13-4-18
 */
@Repository("userDao")
public class UserDao extends BaseHibernateDao<User, Long> implements IUserDao {
	@Override
	public Long getPrincipalAndCredentials(String username, String password) {
		Query query = createQuery("select count(*) from User u where u.username = :username and u.password = :password");
		query.setString("username", username);
		query.setString("password", password);
		return (Long) query.uniqueResult();
	}

	@Override
	public User getUserAllRolsAndPermission(String username) {
		Query query = createQuery("from User u join fetch u.roles r join fetch r.permission p where u.username=:username");
		query.setString("username", username);
		return (User) query.uniqueResult();
	}

	@Override
	public Long getUserCountByUserName(String username) {
		Query query = createQuery("select count(*) from User u where u.username = :username");
		query.setString("username", username);
		return (Long) query.uniqueResult();
	}

	@Override
	public User getUserByUserName(String username) {
		Query query = createQuery("from User u where u.username=:username");
		query.setString("username", username);
		return (User) query.uniqueResult();
	}
}
